package com.testdemo.springboot_demo.controller;

import com.testdemo.springboot_demo.pojo.User;
import com.testdemo.springboot_demo.pojo.ResponseMessage;
import com.testdemo.springboot_demo.pojo.dto.UserDTO;
import com.testdemo.springboot_demo.service.InterfaceUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    InterfaceUserService userService;
    /*
    * 增加
     */
    @PostMapping()  // URL: http://localhost:8080/      method: POST
    public ResponseMessage<User> add(@Validated @RequestBody UserDTO user){
        User userNew = userService.add(user);
        return ResponseMessage.success(userNew);
    }
    /*
     * 查询
     */

    @GetMapping("/{userId}")
    public ResponseMessage<User> get(@PathVariable Integer userId){
        User userNew = userService.getUser(userId);
        return ResponseMessage.success(userNew);
    }
    /*
    * 修改
     */

    @PutMapping
    public ResponseMessage<User> edit(@Validated @RequestBody UserDTO user){
        User userNew = userService.edit(user.getUserId(), user);
        return ResponseMessage.success(userNew);
    }

    /*
     * 删除
     */

    @DeleteMapping("/{userId}")
    public ResponseMessage<User> delete(@PathVariable Integer userId){
        userService.delete(userId);
        return ResponseMessage.success();
    }



}
